const path = require('path')
const fs = require('mz/fs')
const mime = require('mime')

module.exports =  (url, dir) => {
    return async (ctx, next)=>{
        let rpath = ctx.request.path
        if(rpath.startsWith(url)){
            let fpath = path.join(dir + rpath.substring(url.length - 1))
            if (await fs.exists(fpath)){
                ctx.response.type = mime.getType(rpath);
                let file = await fs.readFile(fpath)
                ctx.response.body = file
            }else{
                ctx.response.status = 404
            }
        }else{
            next()
        }
    }
}

